package top.wettis.chat.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.util.SaResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import top.wettis.chat.constant.ResultType;
import top.wettis.chat.pojo.DO.User;
import top.wettis.chat.pojo.Query.UpdateAccount;
import top.wettis.chat.pojo.Query.UpdatePasswordAccount;
import top.wettis.chat.pojo.Query.UserAccount;
import top.wettis.chat.pojo.VO.Result;
import top.wettis.chat.service.IUserService;
import top.wettis.chat.service.impl.UserServiceImpl;

import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author wettis
 * @since 2023-03-25
 */
@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/user")
@Slf4j
public class UserController {
    private final UserServiceImpl userService;

    @Autowired
    private IUserService iUserService;


    public UserController(UserServiceImpl userService) {
        this.userService = userService;
    }

    @PostMapping("/login")
    public SaResult Login(@RequestBody UserAccount userAccount) {
        return userService.doLogin(userAccount);
    }

    @GetMapping("/isLogin")
    public Result<Object> IsLogin() {
        return userService.isLogin();
    }

    @GetMapping("/logout")
    public Result<Object> Logout() {
        return userService.logout();
    }

    @GetMapping("/tokenInfo")
    public Result<Object> TokenInfo() {
        return userService.tokenInfo();
    }

    @PostMapping("/register")
    public Result<Object> Register(@RequestBody UserAccount userAccount) {
        return userService.register(userAccount);
    }

    @DeleteMapping("/remove/{id}")
    @Transactional
    public Result<Object> Remove(@PathVariable Integer id) {
        return userService.deleteall(id);
    }


    @PostMapping("/list")
    public Result<Object> page(@RequestBody User user) {
        List<User> users = userService.userPage(user);
        return Result.successInfo(ResultType.SAVE, users);
    }

    @GetMapping("/one")
    @ResponseBody
    public Result<Object> selectOne(@RequestParam("id") Integer id) {
        return userService.selectById(id);
    }

//    @PutMapping("/update")
//    @ResponseBody
//    public Result<Object> update(@RequestBody User user) {
//        return iUserService.userUpdate(user);
//    }

    @PutMapping("/updatepassword")
    @ResponseBody
    public Result<Object> updatepassword(@RequestBody UpdatePasswordAccount updatePasswordAccount) {
//        int uid = Integer.parseInt(id);
        return userService.updatepassword(updatePasswordAccount.getUserId());
    }

    @PostMapping("/save_update")
    @ResponseBody
    public Result<Object> save(@RequestBody User user) {
        if (user.getUserId() == null) {
            boolean save = iUserService.add(user);
            if (save) {
                return Result.successInfo(ResultType.SAVE, "添加成功");
            } else {
                return Result.failInfo(ResultType.SAVE, "该用户已存在");
            }
        } else {
            boolean update = iUserService.userUpdate(user);
            if (update) {
                return Result.successInfo(ResultType.UPDATE, "修改成功");
            } else {
                return Result.failInfo(ResultType.UPDATE, "修改失败或该用户不存在");
            }
        }
    }


}
